<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../public/vue.js"></script>
</head>
<body>
<div id="example">
    <p>Original message: "{{ message }}"</p>
    <p>Computed reversed message: "{{ reversedMessage }}"</p>

    <p>{{ message }}</p>
    <button v-on:click="printLog">输出</button>
    <button v-on:click="changeMsg">change</button>
</div>
</body>
<script type="text/javascript">
    const vm = new Vue({
        el: '#example',
        data: {
            message: 'Hello'
        },
        computed: {
            // 计算属性的 getter
            reversedMessage: function () {
                // `this` 指向 vm 实例，计算属性内调用方法
                this.printLog2()
                return this.message.split('').reverse().join('')
            }
        },
        methods: {
            printLog: function () {
                // 方法内调用计算属性的缓存值
                console.log("方法内调用计算属性", this.reversedMessage)
            },
            printLog2: function () {
                console.log("计算属性内调用方法", "777")
            },
            changeMsg: function () {
                // 方法内更新计算属性缓存
                this.message = (Math.ceil(Math.random() * 10)).toString() + "方法内更新缓存值"
            }
        }
    });
</script>
</html>